package com.bitrate.dagger.kaptcha.config;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Properties;

import javax.imageio.ImageIO;

import com.bitrate.dagger.kaptcha.config.KaptchaProperties.BackgroundColor;
import com.bitrate.dagger.kaptcha.config.KaptchaProperties.Border;
import com.bitrate.dagger.kaptcha.config.KaptchaProperties.Content;
import com.bitrate.dagger.kaptcha.config.KaptchaProperties.Font;
import com.bitrate.dagger.kaptcha.dto.KaptchaResponseDTO;
import com.bitrate.dagger.kaptcha.enums.KaptchaChannel;
import com.bitrate.dagger.kaptcha.spi.IKaptchaDriver;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

import lombok.extern.slf4j.Slf4j;

/**
 * 谷歌图验驱动
 */
@Slf4j
public class GoogleKaptchaDriver implements IKaptchaDriver {

	@Override
	public String channel() {
		return KaptchaChannel.GOOGLE.getChannel();
	}

	@Override
	public KaptchaResponseDTO execute(KaptchaProperties properties) {
		log.info("SPI选择Google图验生成器,开始生成图片验证码");
		Properties prop = new Properties();
		prop.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, String.valueOf(properties.getWidth()));
		prop.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, String.valueOf(properties.getHeight()));

		Content content = properties.getContent();
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, content.getSource());
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, String.valueOf(content.getLength()));
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, String.valueOf(content.getSpace()));

		BackgroundColor backgroundColor = properties.getBackgroundColor();
		prop.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_FROM, backgroundColor.getFrom());
		prop.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_TO, backgroundColor.getTo());

		Border border = properties.getBorder();
		prop.setProperty(Constants.KAPTCHA_BORDER, border.getEnabled() ? "yes" : "no");
		prop.setProperty(Constants.KAPTCHA_BORDER_COLOR, border.getColor());
		prop.setProperty(Constants.KAPTCHA_BORDER_THICKNESS, String.valueOf(border.getThickness()));

		Font font = properties.getFont();
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, font.getName());
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, String.valueOf(font.getSize()));
		prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, font.getColor());
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		defaultKaptcha.setConfig(new Config(prop));
		KaptchaResponseDTO dto = new KaptchaResponseDTO();
		String base64Image = "";
		String code = defaultKaptcha.createText();
		BufferedImage bfImage = defaultKaptcha.createImage(code);
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		try {
			ImageIO.write(bfImage, "jpg", bs);
			base64Image = Base64.getEncoder().encodeToString(bs.toByteArray());
			bs.close();
		} catch (IOException e) {
			e.printStackTrace();
			return dto.setFlag(false);
		}
		dto.setBase64Image(base64Image);
		dto.setCode(code);
		return dto;
	}

}
